/**
 * 
 */
package cz.jprmr.lorn.client.pages.loginpage;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import cz.jprmr.lorn.client.pages.Page;
import cz.jprmr.lorn.client.utils.Md5;
import cz.jprmr.lorn.client.LoginService;
import cz.jprmr.lorn.client.LoginServiceAsync;
import cz.jprmr.lorn.client.User;

/**
 * @author bredy
 *
 */
public class LoginPage extends Page {	

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final LoginServiceAsync loginService = GWT.create(LoginService.class);
	
	interface MyUiBinder extends UiBinder<Widget, LoginPage> {}
	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
	
	@UiField Button button;
	@UiField TextBox loginName;
	@UiField PasswordTextBox password;

	public LoginPage() {
		System.out.println("LoginPage c");		
		initWidget(uiBinder.createAndBindUi(this));
		
		button.addClickHandler(new LoginHandler());
	}
	
	@Override
	protected void onLoad() {
		super.onLoad();
	}
	
	class LoginHandler implements ClickHandler {
		
		public void onClick(ClickEvent event) {
			System.out.println("Login click");
			User newUser = new User();
			newUser.setLogin(loginName.getText());
			newUser.setPassword(Md5.hash(password.getText()));
			
			loginService.login(newUser, new AsyncCallback<Boolean>() {

				public void onFailure(Throwable caught) {
					System.out.println("Login error.");					
				}

				public void onSuccess(Boolean result) {
					System.out.println("Recieved response: "+result);					
				}
			});
		}
	}
}


